package com.module.prms.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;

import com.module.prms.vo.FMenuTree;
import com.module.prms.vo.MenuTree;
import com.module.prms.vo.PrmsMenu;
import com.module.prms.vo.PrmsOperation;
import com.module.prms.vo.PrmsRoleOperation;
import com.module.prms.vo.RoleOptVo;


public class RoleOperationDao extends
		GenericHibernateDao<PrmsRoleOperation, String> {
	
	private RoleOperationDao(){}
	
	private static RoleOperationDao dao;
	
	public synchronized static RoleOperationDao getInstance(){
		if(dao == null )
			dao = new RoleOperationDao();
		return dao;
	}

	/**
	 * 获取用户角色的功能权限菜单
	 * @param roldIds
	 * @return
	 */
	public FMenuTree getmenuopt(Session session,PrmsRoleOperation role) {
		String hql="";
		String roleId="'"+role.getCroleId()+"'";
		if(role.getCorgId().equals("0")){//超级管理员
			hql = "from PrmsMenu as obj where obj.cmenuPid='0'";
		}else{
			hql = " from PrmsMenu as obj where obj.cmenuId in(" +
			"select cmenuPid from PrmsMenu where cmenuId in (" +
			"select cmenuId from PrmsRoleOperation where croleId in" +
			"("+roleId+")))";
		}
		Query query = session.createQuery(hql.toString());
		List<MenuTree> menutree = new ArrayList();
		List<PrmsMenu> pmenu = query.list();
		for (PrmsMenu pm : pmenu) {//子菜单
			if(role.getCorgId().equals("0")){
				hql = "from PrmsMenu as obj where obj.cmenuPid='" + pm.getCmenuId()
				+ "'";
			}else{
				hql = "from PrmsMenu as obj where obj.cmenuPid='" + pm.getCmenuId()
				+ "' and obj.cmenuId in(" +
				"select cmenuId from PrmsRoleOperation where croleId in" +
				"("+roleId+")))";
			}
			query = session.createQuery(hql.toString());
			List<PrmsMenu> cmenu = query.list();
			List<RoleOptVo> roList = new ArrayList();
			for (PrmsMenu pmopt : cmenu) {
				RoleOptVo rovo = new RoleOptVo();
				rovo.setMenuid(pmopt.getCmenuId());
				rovo.setMenuname(pmopt.getCmenuName());
				rovo.setMenupid(pmopt.getCmenuPid());
				hql = "from PrmsOperation as obj where obj.cmenuId='"
						+ pmopt.getCmenuId() + "'";
				query = session.createQuery(hql.toString());
				List<PrmsOperation> optlist = query.list();
				rovo.setChildren(optlist);
				roList.add(rovo);
			}
			MenuTree mt = new MenuTree();
			mt.setCcreaterId(pm.getCcreaterId());
			mt.setCmenuId(pm.getCmenuId());
			mt.setCmenuName(pm.getCmenuName());
			mt.setCmenuPid(pm.getCmenuPid());
			mt.setDcreateTime(pm.getDcreateTime());
			mt.setChildren(roList);
			menutree.add(mt);

		}
		FMenuTree fmt = new FMenuTree();
		fmt.setTree(menutree);
		return fmt;
	}
	
	//返回所有的按钮编号
	public List<String> getOperationByRoleIdAndMenuId(Session session,String roleId,String menuId){
		List<String> list= new ArrayList<String>();
		String hql = " from PrmsRoleOperation  where croleId='"+roleId+"' and cmenuId='"+menuId+"'";
		Query query = session.createQuery(hql.toString());
		List<PrmsRoleOperation> listpro = query.list();
		for(int i=0;i<listpro.size();i++){
			String temp=listpro.get(i).getCoperationId();
			list.add(temp);
		}
		return list;
	}
	
	
	
	
	/**
	 *将操作放入FMenuTree 里的 list里。
	 * */
	public FMenuTree getroleopt(Session session,String roleid)
	{
		String hql = "from PrmsRoleOperation as obj where obj.croleId='"+roleid+"'";
		Query query = session.createQuery(hql.toString());
		List<PrmsRoleOperation> roleopt = query.list();
		FMenuTree fmt = new FMenuTree();
		fmt.setTree(roleopt);
		return fmt;
	}
	
	
	
	
public static void main (String args[])
{
	RoleOperationDao rod = new RoleOperationDao();
	//rod.getroleopt("40288509318e630d01318e6361f50001");
}
	
	
	
}
